//
// Created by kevin on 2024/11/30.
//
#include<string>
#include<iostream>
#include "lte_cellRs.h"
#include "tx_modulationQAM.h"

using namespace std;
// 比较函数，用于排序时自定义比较规则（这里针对基本数据类型，可按需修改）
template<typename T>
bool compare(const T& a, const T& b) {
    return a < b;
}

// 自定义的unique函数，模拟Matlab的unique功能
// 返回值为pair类型，first成员存放唯一值的数组，second成员存放对应在原数组中的索引数组
pair<vector<int>, vector<int>> unique(const vector<int>& input) {
    vector<int> sortedInput = input;
    // 对输入数组进行排序，方便后续查找重复元素
    sort(sortedInput.begin(), sortedInput.end(), compare<int>);

    vector<int> uniqueValues;
    vector<int> indices;
    if (sortedInput.size() > 0) {
        uniqueValues.push_back(sortedInput[0]);
        indices.push_back(0);
        for (size_t i = 1; i < sortedInput.size(); ++i) {
            if (sortedInput[i]!= sortedInput[i - 1]) {
                uniqueValues.push_back(sortedInput[i]);
                indices.push_back(i);
            }
        }
    }
    return make_pair(uniqueValues, indices);
}

void lte_cellRS(sTM tmConfig,vector<unsigned int> *cellRSindices,int cell_rows, int cell_cols, int nsubframe){
    unsigned N_cell_ID = tmConfig.NCellID;
    unsigned Ncp = 1;
    unsigned N_ofdsm_slot = 7;
    auto curr = cellRSindices[1];
    auto l_all = unique(curr);

    int len = l_all[0].length();
    for(int i = 0; i < len; i++){
        auto l_rs = l_all[i] % N_ofdsm_slot;

        auto indexSlotInFrame = 2 * nsubframe + (l_rs >= N_ofdsm_slot);
        auto initC = 1024 * (7*(indexSlotInFrame + 1) + (l_rs) + 1) * (2*N_cell_ID + 1) + 2*N_cell_ID + Ncp;

        auto N_RB_Max = 110;
        auto sequenceLength = 2 * 2 * N_RB_Max;

        auto glodsequencetmp = tx_PRBSGeneration_lte(initC,sequenceLength);

        auto NDLRB = tmConfig.NDLRB;

        {
            int begin = sequenceLength / 2 - 2*NDLRB+1;
            int end = sequenceLength / 2 + 2*NDLRB;
            int len = end - begin;
            u8* ptr = (u8*)&glodsequencetmp[begin];

            double* sequencetmp1 = tx_modulationQAM(ptr, len, 2 );
        }
    }
}